;;; duplicated from p22.lisp
(defun alphabetical-value (word &optional (cal-op '+))
  (reduce cal-op
    (map 'vector
	     (lambda (x) (1+ (- (char-int x) (char-int #\A))))
	     (string word))))

;;; TODO 
;;; define function to predicate if the number is triangle-sequence
"""
1.
(defun triangle-num-p (n)
  (integerp (/ (+ -1 (sqrt (+ 1 (* 8 n)))) 2)))
 because  x^2 + x - 2n = 0
"""
(defun triangle-sequence (n)
  (loop for i from 1 to n
        collect (/ (* i (1+ i)) 2)))

;;; This longest length of a single word is 11, thus the biggest word value is 26*15 < 400
(defparameter *input-data* 
  (with-open-file (f (merge-pathnames #P"p42_words.txt"))
      (loop for name = (read f nil) while name 
		  collect name)))

(defun p42 (&optional (data *input-data*))
;  (sort data (lambda (x y) (< (length x) (length y))))
;  (print data)
  (let ((predef-triangle-list (triangle-sequence 30)))
    (loop for i from 1 to (length data)
          summing (count (alphabetical-value (nth (1- i) data)) predef-triangle-list))))

;(format t "~a~%" (triangle-sequence 30))
;(format t "~a~%" (count 2199 (triangle-sequence 24)))
(format t "~a~%" (p42))